$(document).ready(function() {
	
	$("#selectall").click(function() {
        if ($("#selectall").prop("checked") == true) {
            $(".chek_all").prop("checked", true);
        } else {
            $(".chek_all").prop("checked", false);
        }
    });
	
	$(".form_datetime").datepicker({
		format: 'yyyy-mm-dd'
	});
	
	$("#quote_com_id").select2({
       placeholder: "Select Company",
	  width: '100%'
    });
	
	$("#quote_clnt_id").select2({
       placeholder: "Select Client",
	  width: '100%'
    });
	
	$("#quote_deal_id").select2({
       placeholder: "Select Deal",
	  width: '100%'
    });
	
	$("#quote_main_status").select2({
       placeholder: "Select Quote Status",
	  width: '100%'
    });
	
	/*$("#comp_industry").select2({
       placeholder: "Select Company Industry",
	   width: '300px'
    });
	
	$("#comp_type").select2({
       placeholder: "Select Company Type",
	   width: '300px'
    });*/
	
	// add dropzone
	Dropzone.autoDiscover = false;
	var paramsdata = {};
	paramsdata['UNIQUEIDENTIFIER'] = UNIQUEIDENTIFIER;
		
	$(".qf_gallery").dropzone({
		url: ADMIN_URL + 'quotes/upload/',
		paramName: 'Quotes',
		params: paramsdata,
		maxFilesize: 500,
		acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
	});
  
	// Company Popup
	$("#add_company").click(function() {
        var chkArray = [];
        $(".chek_all:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',');
        //$("#ac_property_id").val(selected);
        $("#myModal_company").modal('show');

    });
	
	$("#add_btn_company").click(function() {
		
        var comp_name = $("#comp_name").val();
		var comp_type = $("#comp_type").val();
		var comp_industry = $("#comp_industry").val();
		var comp_email = $("#comp_email").val();
		var comp_phone = $("#comp_phone").val();
		var comp_adrline1 = $("#comp_adrline1").val();
        $("#error_add_client").hide();
		
		if (comp_name == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter company name');
            $("#comp_name").css('border-color', 'red');
            return false;
        } else if (comp_type == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please select company type');
            $("#comp_type").css('border-color', 'red');
            return false;
        } else if (comp_industry == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please select industry');
            $("#comp_industry").css('border-color', 'red');
            return false;
        } else if (comp_email == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter email');
            $("#comp_email").css('border-color', 'red');
            return false;
        } else {
            var postdata = {};
            var obj = $(this);
            postdata['comp_name'] = comp_name;
			postdata['comp_type'] = comp_type;
			postdata['comp_industry'] = comp_industry;
			postdata['comp_email'] = comp_email;
			postdata['comp_phone'] = comp_phone;
			postdata['comp_adrline1'] = comp_adrline1;
			
			jQuery.post(ADMIN_URL + 'quotes/addcompany', postdata, function(company_id) {
               
				$("#success_add_client").show();
				window.setTimeout(function() {
					$(".chek_all").prop("checked", false);
					$("#success_add_client").hide();
					$("#myModal_company").modal('hide');
				}, 3000);
			
				var postdata = {};
				postdata['company_last_id'] = company_id;
				jQuery.post(ADMIN_URL + 'quotes/getcompanylist', postdata, function(result) {
				   $('#quote_com_id').html(result);
				   $("#quote_com_id").select2("val", company_id);
				});
             
            });
        }
    });
	
	// Client Popup
	$("#add_client").click(function() {
        var chkArray = [];
        $(".chek_all:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',');
        //$("#ac_property_id").val(selected);
        $("#myModal_client").modal('show');

    });
	
	$("#add_btn_client").click(function() {
		
        var clnt_first_name = $("#clnt_first_name").val();
		var clnt_middle_name = $("#clnt_middle_name").val();
		var clnt_last_name = $("#clnt_last_name").val();
		var clnt_email = $("#clnt_email").val();
		var clnt_phone = $("#clnt_phone").val();
		
        $("#error_add_client").hide();
		
		if (clnt_first_name == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter first name');
            $("#clnt_first_name").css('border-color', 'red');
            return false;
        } else if (clnt_last_name == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter last name');
            $("#clnt_last_name").css('border-color', 'red');
            return false;
        } else if (clnt_email == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter email');
            $("#comp_industry").css('border-color', 'red');
            return false;
        } else if (clnt_phone == '') {
            $("#error_add_client").show();
            $("#error_add_client").html('Please enter phone no.');
            $("#clnt_phone").css('border-color', 'red');
            return false;
        } else {
            var postdata = {};
            var obj = $(this);
            postdata['clnt_first_name'] = clnt_first_name;
			postdata['clnt_middle_name'] = clnt_middle_name;
			postdata['clnt_last_name'] = clnt_last_name;
			postdata['clnt_email'] = clnt_email;
			postdata['clnt_phone'] = clnt_phone;
			
			jQuery.post(ADMIN_URL + 'quotes/addclient', postdata, function(clnt_id) {
              
				$("#success_add_client").show();
				window.setTimeout(function() {
				   $(".chek_all").prop("checked", false);
					$("#success_add_client").hide();
					$("#myModal_client").modal('hide');
				}, 3000);	
				
				var postdata = {};
				postdata['client_last_id'] = clnt_id;
				jQuery.post(ADMIN_URL + 'quotes/getclientlist', postdata, function(result) {
				   $('#quote_clnt_id').html(result);
				   $("#quote_clnt_id").select2("val", clnt_id);
				});
            });
        }
    });
});